<% layout('../../includes/layout-form-v2') -%>
<div class="box-body">


	<form id="form" class="bid-pack"
		action='/frontend/<%-controller%>/<%- action %>/<%- id %>'
		method='post'>
		
		
		<div class="row">
			
			<div class="col-lg-3">
				<div class="input-group">
					<span class="input-group-addon"> Username </span> <input type="text" class="form-control" id="username">
				</div>
				<!-- /input-group -->
			</div>
			<!-- /.col-lg-2 -->

			<div class="col-lg-3">
				<div class="input-group">
					<span class="input-group-addon"> First Name </span> <input type="text" class="form-control" id="firstname">
				</div>
				<!-- /input-group -->
			</div>
			<!-- /.col-lg-2 -->

			<div class="col-lg-3">
				<div class="input-group">
					<span class="input-group-addon"> Last Name </span> <input type="text" class="form-control pull-right" id="lastname">
				</div>
				<!-- /input-group -->
			</div>
			<!-- /.col-lg-2 -->

			



			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<input type="submit" class="btn btn-primary btn searchBidpack" value="Search">
		</div>
		
		
		
	</form>

	
	<br />
	<table class="table table-bordered" id="bidpackTable">
		<tbody >
			<tr>
				<th>Username</th>
				<th>Email</th>
				<th>First Name</th>
				<th>Last Name</th>
				<th>Bid Pack</th>
			</tr>
		</tbody>
		<tbody id="tableData">
		
		</tbody>
	</table>
	
	
	
	<div class="box box-primary pull-left BidpackUserInfo">
		<form id="form" class="buyBidpack" method="post">
			<div class="box-header">
				<h3 class="box-title">User Info</h3>
			</div><!-- /.box-header -->
			
			<div class="col-xs-8" >
			
				<div class="input-group">
	                <span class="input-group-addon">Bid Pack</span>
	                <select class="form-control" id="bidpackName">

	                </select>
	            </div><br/>
	            
	            <div class="input-group">
		           	<label>Price : </label>
		            <small class="text-green" id="bidpackPrice" style="font-size: medium; margin-left: 5px;"></small>
	            </div><br/>
	            
	            <div class="input-group">
	                <span class="input-group-addon ">Username</span>
	                <input type="text" class="form-control" id="bidpackUsername" disabled>
	            </div><br/>
	            
	            <div class="input-group">
	                <span class="input-group-addon ">Email</span>
	                <input type="text" class="form-control text-light-blue" id="bidpackEmail" disabled>
	            </div><br/>
	            
	            <div class="input-group">
	                <span class="input-group-addon ">First Name</span>
	                <input type="text" class="form-control" id="bidpackFirstName" disabled>
	            </div><br/>
	            
	            <div class="input-group">
	                <span class="input-group-addon ">Last Name</span>
	                <input type="text" class="form-control" id="bidpackLastName" disabled>
	            </div><br/>
	            
	            <div class="input-group">
	                <span class="input-group-addon ">Note</span>
	               <textarea class="form-control" id="bidpackNote"></textarea>
	            </div><br/>
	            
	            <input type="hidden" id="bidpackUserId" value="" >
				
				
				<div class="form-group pull-left">
					&nbsp;&nbsp;
					<label><input type="submit" value="Submit" class="btn btn-success buyBidpackBTN">  </label>
					<small ></small>
				</div> </br>
			</div>

		</form>
	</div><!-- /.box-primary -->

</div>
<!-- /.box-body -->

<script>


var userResult  = {};

$(document).on("click", ".bidpackVal", function(e) {

	$(".buyBidpackBTN").show();
	
	$("#bidpackTable").hide();
	$(".BidpackUserInfo").show();
	//console.log(userResult[$(this).attr("value")]['username']);
	$( "#bidpackUsername" ).val(userResult[$(this).attr("value")]['username']);
	$( "#bidpackEmail" ).val(userResult[$(this).attr("value")]['email']);
	$( "#bidpackFirstName" ).val(userResult[$(this).attr("value")]['firstname']);
	$( "#bidpackLastName" ).val(userResult[$(this).attr("value")]['lastname']);
	
	$("#bidpackUserId").val(userResult[$(this).attr("value")]['id']);
	
	
	
	var parameters = {
			search_for : 'bidpack'
		};
		
		$.ajax({
			url : "<%= refData.settingsList[0]['url'] %>",
			type : 'get',
			data : parameters,
			dataType : 'json',

			success : function(json) {

				
				if($("#bidpackName").val() == null) {
					for(var i = 0; i < json.length; i++) {
						console.log(json);
						var x = new Option(json[i]['bidpack_name'], json[i]['id']);
						x.setAttribute("id", json[i]['bid_price']);
						$("#bidpackPrice").html(json[0]['bid_price']);
						$("#bidpackName").append(x);
						
					}
				}
				
				
			},

			error : function(json) {
				console.log("Something's wrong!");
			}

		});

});


$(document).on("change", "#bidpackName", function(e) {
	var id = $(this).children(":selected").attr("id");
	$("#bidpackPrice").html(id);
});


$(document).on("submit", ".buyBidpack", function(e){
	e.preventDefault();
	
	if($("#bidpackNote").val() == ""){
		alert("Note is required.");
		return false;
	}
	
	var parameters = {
			submit_to : 'payment_order',
			bidpack_id : $('#bidpackName').val(),
			branch_id : <%- currentBranchId %>,
			userid : $("#bidpackUserId").val(),
			company_key : "<%- currentCompanyKey %>",
			note : $('#bidpackNote').val()
		};
	
		
	$(".buyBidpackBTN").attr("disabled", true);

	$.ajax({
		type : "post", 
		url : "<%= refData.settingsList[0]['url'] %>",
		data : parameters,
		dataType : 'json',
		success : function(msg) {

			if(msg.success) {
				$(".buyBidpackBTN").attr("disabled", false);
				$(".buyBidpackBTN").hide();
				$("#bidpackNote").val("");
				alert(msg.success);
			} else {
				$(".buyBidpackBTN").attr("disabled", false);
				alert("Error in database");
			}
			
			console.log(msg);
		},
		error : function(msg) {
			$(".buyBidpackBTN").attr("disabled", false);
			alert("Error in database");
			console.log("Something's wrong!");
		}
	});
	
});


$(document).on("submit", ".bid-pack", function(e) {
	e.preventDefault();
	// Bid Pack
	
	$(".searchBidpack").attr("disabled", true);
	
	$(".BidpackUserInfo").hide();
	
	var parameters = {
		search_for : 'registration',
		username : $("#username").val(),
		firstname : $("#firstname").val(),
		lastname : $("#lastname").val()
	};

	$.ajax({
		url : "<%= refData.settingsList[0]['url'] %>",
		type : 'get',
		data : parameters,
		dataType : 'json',

		success : function(json) {
			
			userResult = json;
			$(".searchBidpack").attr("disabled", false);
			$("#bidpackTable").show();
			
			console.log(json);
			
			$("#tableData").html("");

			for (var i = 0; i < json.length; i++){
			    

				
				var tr = document.createElement('tr');
	
				//create table data
				var tdUsername = document.createElement('td');
				var tdEmail = document.createElement('td');
			    var tdFirstname = document.createElement('td');
			    var tdLastname = document.createElement('td');
			    var tdBidpack = document.createElement('td');
			    var btnBidpack = document.createElement('button');
			    var tdId = document.createElement('td');
			    
			    
			    
			    //add class name
			    btnBidpack.className = "btn btn-info bidpackVal";
			    //btnBidpack.value = [json[i]['id'], json[i]['username']];
			    btnBidpack.value = i;
			    
			    tdId.className = "wonAuctionId " + "wonAuctionId"+json[i]['id'];
				
			    
			    
			  	//create table text node
			    var username = document.createTextNode(json[i]['username']);
			    var email = document.createTextNode(json[i]['email']);
			    var firstname = document.createTextNode(json[i]['firstname']);
			    var lastname = document.createTextNode(json[i]['lastname']);
			    var id = document.createTextNode(json[i]['id']);
			    
			    tdUsername.appendChild(username);
			    tdEmail.appendChild(email);
			    tdFirstname.appendChild(firstname);
			    tdLastname.appendChild(lastname);
			    tdBidpack.appendChild(btnBidpack);
			    tdId.appendChild(id);
			    
			    tr.appendChild(tdUsername);
			    tr.appendChild(tdEmail);
			    tr.appendChild(tdFirstname);
			    tr.appendChild(tdLastname);
			    tr.appendChild(tdBidpack);
			    tr.appendChild(tdId);

			    document.getElementById('tableData').appendChild(tr);
			}
			
			$(".bidpackVal").text("Buy Bid Pack");
			
			if(json.error) {
				$("#bidpackTable").hide();
				alert(json.error);
			}
			
			

		},

		error : function(json) {
			$("#bidpackTable").hide();
			$(".searchBidpack").attr("disabled" , false);
			alert("Error in Database.");
			console.log("Something's wrong!");
		}

	});

});
</script>